package cn.hairui.blog.controller.manage;

import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.model.User;
import cn.hairui.blog.model.manage.MgrResc;
import cn.hairui.blog.model.manage.MgrRescmenu;
import cn.hairui.blog.service.manage.ResourceService;
import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author lihairui
 * @version V1.0
 * @date: 2019/8/4 16:23
 */
@Controller
@RequestMapping(value = "/manage")
public class ResourceManageController {
    @Autowired
    private ResourceService resourceService;

    @RequestMapping(value = "/navs")
    @ResponseBody
    public String navs() {
        //获取左侧菜单
        List<MgrResc> mgrRescList = resourceService.listRescMgrShow();
        Iterator iterator = mgrRescList.iterator();
        Map map = new HashMap();
        while (iterator.hasNext()) {
            MgrResc mgrResc = (MgrResc) iterator.next();
            String rescId = mgrResc.getRescId();
            List<MgrRescmenu> mgrRescmenuList = resourceService.findMgrRescmenuByrescId(rescId);
            map.put(rescId, mgrRescmenuList);
        }
        JSONObject json = new JSONObject(map);
        return json.toString();
    }

    //mgrResc列表页面
    @RequestMapping(value = "page/mgrResc/mgrRescList")
    public String mgrRescList() {
        return "manage/page/mgrResc/mgrRescList";
    }

    //mgrRescmenu列表页面
    @RequestMapping(value = "page/mgrRescmenu/mgrRescmenuList")
    public String mgrRescmenuList() {
        return "manage/page/mgrRescmenu/mgrRescmenuList";
    }


    //mgrResc列表数据
    @RequestMapping(value = "page/mgrResc/list")
    @ResponseBody
    public String listMgrResc(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int pageSize = 15;//默认每页显示6条记录
        int pageNum = 1;
        try {
            pageNum = Integer.parseInt(request.getParameter("page"));
            pageSize = Integer.parseInt(request.getParameter("limit"));
        } catch (Exception e) {
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Map> mapList = resourceService.listMgrRescMgr();
        PageInfo<Map> pageInfo = new PageInfo<Map>(mapList);
        map.put("data", mapList);
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return JSONUtils.toJSONString(map);
    }

    //mgrRescmenu列表数据
    @RequestMapping(value = "page/mgrRescmenu/list")
    @ResponseBody
    public String listMgrRescmenu(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        List<Map> mapList = resourceService.listMgrRescmenuMgr();
        map.put("data", mapList);
        map.put("count", mapList.size());
        map.put("code", 0);
        return JSONUtils.toJSONString(map);
    }

    //mgrResc新增页面
    @RequestMapping(value = "page/mgrResc/mgrRescAdd")
    public String mgrRescAdd(Model model) {
        return "manage/page/mgrResc/mgrRescAdd";
    }

    //mgrRescmenuAdd新增页面
    @RequestMapping(value = "page/mgrRescmenu/mgrRescmenuAdd")
    public String mgrRescmenuAdd(Model model) {
        List<MgrResc> mgrRescs = resourceService.listRescMgrShow();
        model.addAttribute("mgrRescs", mgrRescs);
        return "manage/page/mgrRescmenu/mgrRescmenuAdd";
    }


    //mgrResc新增数据
    @RequestMapping(value = "page/mgrResc/add")
    @ResponseBody
    public String addMgrResc(HttpServletRequest request, MgrResc mgrResc) {
        Map map = new HashMap<Object, Object>();
        try {
            resourceService.addMgrResc(mgrResc);
            map.put(PubConstant.flag, PubConstant.success);
        } catch (Exception e) {
            map.put(PubConstant.flag, PubConstant.failed);
        }
        return JSONUtils.toJSONString(map);
    }

    //mgrRescmenu新增数据
    @RequestMapping(value = "page/mgrRescmenu/add")
    @ResponseBody
    public String addMgrRescmenu(HttpServletRequest request, MgrRescmenu mgrRescmenu) {
        Map map = new HashMap<Object, Object>();
        try {
            resourceService.addMgrRescmenu(mgrRescmenu);
            map.put(PubConstant.flag, PubConstant.success);
        } catch (Exception e) {
            map.put(PubConstant.flag, PubConstant.failed);
        }
        return JSONUtils.toJSONString(map);
    }

    //修改页面
    @RequestMapping(value = "page/mgrResc/mgrRescUpdate")
    public String mgrRescUpdate(HttpServletRequest request, Model model) {
        String rescId = request.getParameter("rescId");
        //加载下拉字典
        //首页分类

        MgrResc mgrResc = resourceService.findMgrResc(rescId);
        model.addAttribute("mgrResc", mgrResc);
        return "manage/page/mgrResc/mgrRescUpdate";
    }

    //mgrRescmenu修改页面
    @RequestMapping(value = "page/mgrRescmenu/mgrRescmenuUpdate")
    public String mgrRescmenuUpdate(HttpServletRequest request, Model model) {
        String rescId = request.getParameter("rescId");
        String href = request.getParameter("href");
        //加载下拉字典
        List<MgrResc> mgrRescs = resourceService.listRescMgrShow();
        model.addAttribute("mgrRescs", mgrRescs);

        MgrRescmenu mgrRescmenu = resourceService.findMgrRescmenu(rescId, href);
        model.addAttribute("mgrRescmenu", mgrRescmenu);
        return "manage/page/mgrRescmenu/mgrRescmenuUpdate";
    }


    //mgrResc更新数据
    @RequestMapping(value = "page/mgrResc/update")
    @ResponseBody
    public String updateMgrResc(HttpServletRequest request, MgrResc mgrResc) {
        Map map = new HashMap<Object, Object>();
        resourceService.updateMgrResc(mgrResc);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    //mgrResc更新数据
    @RequestMapping(value = "page/mgrRescmenu/update")
    @ResponseBody
    public String updateMgrRescmenu(HttpServletRequest request, MgrRescmenu mgrRescmenu) {
        Map map = new HashMap<Object, Object>();
        try {
            resourceService.updateMgrRescmenu(mgrRescmenu);
            map.put(PubConstant.flag, PubConstant.success);
        } catch (Exception e) {
            e.printStackTrace();
            map.put(PubConstant.flag, PubConstant.failed);
        }
        return JSONUtils.toJSONString(map);
    }

    //mgrResc删除数据
    @RequestMapping(value = "page/mgrResc/delete")
    @ResponseBody
    public String deleteMgrResc(HttpServletRequest request, String rescId) {
        Map map = new HashMap<Object, Object>();
        Logger.getLogger(String.valueOf(this.getClass())).info("删除资源：" + rescId);
        try {
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(PubConstant.GLOBAL_SESSION_NAME);
            if (user != null && PubConstant.YES_NO_Y.equals(user.getSuperUser())) {
                //这里设置超级管理员才能操作系统资源
                resourceService.deleteMgrResc(rescId);
                map.put(PubConstant.flag, PubConstant.success);
            } else {
                map.put(PubConstant.flag, PubConstant.failed);
            }

        } catch (Exception e) {
            map.put(PubConstant.flag, PubConstant.failed);
        }


        return JSONUtils.toJSONString(map);
    }

    //mgrRescmenu删除数据
    @RequestMapping(value = "page/mgrRescmenu/delete")
    @ResponseBody
    public String deleteMgrRescmenu(HttpServletRequest request, String rescId, String href) {
        Map map = new HashMap<Object, Object>();
        Logger.getLogger(String.valueOf(this.getClass())).info("删除资源：" + rescId + " " + href);
        try {
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(PubConstant.GLOBAL_SESSION_NAME);
            if (user != null && PubConstant.YES_NO_Y.equals(user.getSuperUser())) {
                //这里设置超级管理员才能操作系统资源
                resourceService.deleteMgrRescmenu(rescId, href);
                map.put(PubConstant.flag, PubConstant.success);
            } else {
                map.put(PubConstant.flag, PubConstant.failed);
            }

        } catch (Exception e) {
            map.put(PubConstant.flag, PubConstant.failed);
        }
        return JSONUtils.toJSONString(map);
    }
}
